package io.yuncheng.modules.sys.service.impl;

import io.yuncheng.common.utils.*;
import io.yuncheng.common.utils.DateUtils;
import io.yuncheng.common.utils.Query;
import io.yuncheng.modules.sys.entity.SubCascadePlatformLogDetailVo;
import io.yuncheng.modules.sys.dao.SubCascadePlatformLogDao;
import io.yuncheng.modules.sys.entity.SubCascadePlatformLogEntity;
import io.yuncheng.modules.sys.service.SubCascadePlatformLogService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;


@Service("subCascadePlatformLogService")
public class SubCascadePlatformLogServiceImpl extends ServiceImpl<SubCascadePlatformLogDao, SubCascadePlatformLogEntity> implements SubCascadePlatformLogService {

    @Resource
    SubCascadePlatformLogDao subCascadePlatformLogDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubCascadePlatformLogEntity> page = this.page(
                new Query<SubCascadePlatformLogEntity>().getPage(params),
                new QueryWrapper<SubCascadePlatformLogEntity>()
        );
        return new PageUtils(page);
    }
    @Override
    public List<SubCascadePlatformLogDetailVo> queryListCollectByDeptId(Long deptId){
        QueryWrapper<SubCascadePlatformLogEntity> queryWrapper = new QueryWrapper<>();
        SubCascadePlatformLogEntity entity = new SubCascadePlatformLogEntity();
        entity.setCascadeId(deptId);
        queryWrapper.setEntity(entity);
        queryWrapper.orderByAsc("cascade_time");
        List<SubCascadePlatformLogEntity> list = list(queryWrapper);
        Map<String, List<SubCascadePlatformLogEntity>> stringListMap = list.stream().collect(Collectors.groupingBy(SubCascadePlatformLogEntity::buildSeason,LinkedHashMap::new,Collectors.toList()));
        List<SubCascadePlatformLogDetailVo> detailVos = new ArrayList<>();
        for (Map.Entry<String, List<SubCascadePlatformLogEntity>> stringListEntry : stringListMap.entrySet()) {
            List<SubCascadePlatformLogEntity> value = stringListEntry.getValue();
            if(ObjectUtils.isEmpty(value)){
                continue;
            }
            SubCascadePlatformLogDetailVo subCascadePlatformLogDetailVo = new SubCascadePlatformLogDetailVo();
            subCascadePlatformLogDetailVo.setCascadeTime(DateUtils.getSeason(value.get(0).getCascadeTime()));
            subCascadePlatformLogDetailVo.setCascadeStatusName("数据提交已经完成");
            if (value.get(0).getCascadeStatus().equals(1)) {
                subCascadePlatformLogDetailVo.setCascadeStatusName("数据提交未完成");
            }
            detailVos.add(subCascadePlatformLogDetailVo);
        }
        return detailVos;
    }

    @Override
    public Date queryMaxTime() {
       return subCascadePlatformLogDao.queryMaxTime();
    }

}
